package com.atmyc.yygh.hospital.controller.api;

import com.atmyc.yygh.common.exception.YyghException;
import com.atmyc.yygh.common.helper.HttpRequestHelper;
import com.atmyc.yygh.common.result.Result;
import com.atmyc.yygh.common.result.ResultCodeEnum;
import com.atmyc.yygh.common.utils.MD5;
import com.atmyc.yygh.hospital.service.DepartmentService;
import com.atmyc.yygh.hospital.service.HospitalService;
import com.atmyc.yygh.hospital.service.HospitalSetService;
import com.atmyc.yygh.hospital.service.ScheduleService;
import com.atmyc.yygh.model.hosp.Department;
import com.atmyc.yygh.model.hosp.Hospital;
import com.atmyc.yygh.vo.hosp.DepartmentQueryVo;
import com.atmyc.yygh.vo.hosp.DepartmentVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paraMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 校验签名

        scheduleService.save(paraMap);
        return Result.ok();

    }
    //删除科室
    @PostMapping("department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        //获取传递过来的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");

        departmentService.remove(hoscode,depcode);
        return Result.ok();


    }
    //查询科室
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }
    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名
        String hospSigh = (String)paramMap.get("sign");

        //2 根据传递过来的医院编码，查询数据库，查询签名
        String sign = hospitalSetService.getSignKey(hoscode);

        //把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sign);
        if(!hospSigh.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHospcode(hoscode);
        return Result.ok(hospital);
    }
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //1 获取医院系统传递过来的签名
        String hospSigh = (String)paramMap.get("sign");

        //2 根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String sign = hospitalSetService.getSignKey(hoscode);

        //把数据库查询签名进行MD5加密
//        String signKeyMd5 = MD5.encrypt(sign);
//        if(!hospSigh.equals(signKeyMd5)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        //传输过程中“+”转换位“ ”
        String LogoData = (String)paramMap.get("logoData");
        String logoData = LogoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传过来的科室信息
        Map<String,String[]> requestMap = request.getParameterMap();
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名
        String hospSigh = (String)paramMap.get("sign");

        //2 根据传递过来的医院编码，查询数据库，查询签名
        String sign = hospitalSetService.getSignKey(hoscode);

        //把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sign);
        if(!hospSigh.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service
        departmentService.save(paramMap);
        return Result.ok();

    }
}
